
#include "c_Thread.h"
#include "assert.h"

/**
 * default constructor
 */
wiigi::c_Thread::c_Thread( void )
   :
      m_Thread( 0 )
{
   
   
   return;  
}

/**
 * static entry point for thread
 */
void* wiigi::c_Thread::ThreadEntry( void* Arg )
{  
   c_Thread* Thread;
   Thread = (c_Thread*) Arg;
   
   Thread->main();
   
   return 0;
}


/**
 * pause execution of this thread
 */
wiigi::e_Status wiigi::c_Thread::pause( void )
{
   e_Status Status;
   Status = d_StatusUnkown;
   
   if( checkFlags( d_Running ) )
   {
      s32 RetCode;
      RetCode = LWP_SuspendThread( m_Thread );
      if( 0 == RetCode )
      {
         Status = d_StatusOK;
      }
      else
      {
         Status = d_Failed;
      }
   }
   else
   {
      Status = d_IllegalRequest;
   }
   
   assert( d_StatusUnkown != Status );
   return Status;
}

/**
 * starts execution of thread
 */
wiigi::e_Status wiigi::c_Thread::run( void )
{
   e_Status Status;
   Status = d_StatusUnkown;
   
   if( checkFlags( d_Running ) == 0 )
   {
      s32 RetCode;
      RetCode = LWP_CreateThread(
                  &m_Thread,	/* thread handle */ 
   					wiigi::c_Thread::ThreadEntry,			/* code */ 
   					this,		/* arg pointer for thread */
   					NULL,			/* stack base */ 
   					16*1024,		/* stack size */
   					80				/* thread priority */ 
   					);
      
      
      if( RetCode == 0 )
      {
         setFlag( d_Running );
         Status = d_StatusOK;
      }
      else
      {
         Status = d_Failed;
      }
      
   }
   else
   {
      Status = d_StatusOK;
   }
   
   assert( d_StatusUnkown != Status );
   return Status;
}


